<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="format-detection" content="telephone=no" />
<link href="{$Think.config.RESOURCEURL}wx_assets/css/common.css{$Think.config.RESOURCE_VER}" rel="stylesheet" type="text/css">
<link href="{$Think.config.RESOURCEURL}wx_assets/shenqi/shenqi.css{$Think.config.RESOURCE_VER}" rel="stylesheet">
<link href="{$Think.config.RESOURCEURL}wx_assets/shenqi/jquery-sinaEmotion.css{$Think.config.RESOURCE_VER}" rel="stylesheet">
<script type="text/javascript" src="{$Think.config.RESOURCEURL}wx_assets/js/jquery-1.8.2.min.js{$Think.config.RESOURCE_VER}"></script>
<script type="text/javascript" src="{$Think.config.RESOURCEURL}wx_assets/js/zepto.alert.js{$Think.config.RESOURCE_VER}"></script>
<script type="text/javascript" src="{$Think.config.RESOURCEURL}wx_assets/shenqi/js/jquery-sinaEmotion.js{$Think.config.RESOURCE_VER}"></script>
<script src="{$Think.config.RESOURCEURL}wx_assets/js/util.js{$Think.config.RESOURCE_VER}" type="text/javascript"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script src="{$Think.config.RESOURCEURL}wx_assets/js/wx_share.js{$Think.config.RESOURCE_VER}" type="text/javascript"></script>
</head>
<body>
<div class="artifactIns">
    <div class="title">
        <input type="text" id="title" placeholder="输入标题">
    </div>
    <div class="content">
        <textarea placeholder="说点什么吧..." id="content" onkeydown="artifactIns._maxtxtNum()"></textarea>
        <div id="picDiv" class="picDiv clearfix">
           <!-- <div class="uploadimgbox">
           <del></del>
            <img src="http://oss.mboxone.com/20150703upload/aUYIqv0zaiEeL0sjD9052uzonBL45K8XQikj5aYNpBd9TE_einVsCgsBZcr96hFz.jpg@70w_70h_1e_1c_2o_1sh.jpg">
            </div> -->
            <span id="uploadImage"></span>
        </div>
    </div>
    <div class="pay">
        <div class="txtNum">0/140字</div>
        <dl id="PayBox" class="pay-box">
            <img src="{$Think.config.RESOURCEURL}wx_assets/images/loading.gif">
        </dl>
        <a class="release" id="sendBtn">发布</a>
    </div>
    <script type="text/html" id="PayListTpl">
       <%if(list){%>
        <dt>无红包不分享<a class="view" id="view">查看规则</a></dt>
       <%
        for(var i=0;i<list.length;i++) {
            var item=list[i];
        %>
        <dd data-rechange="<%=item.rechange%>" data-base="<%=item.base%>" <%if(item.require) {%>class="rechange"<%}%>>
            <span class="strong" <%if(!item.rechange) {%>style="padding-top:12px;"<%}%> data-base="<%=item.base%>"><%=item.base%>元</span>
            <%if(item.rechange) {%>
            <span>充值<%=item.rechangetotal%>元</span>
            <%}%>
        </dd>
        <%}%>
        <dd class="account">账户余额：<em>￥<%=miCoins%>元</em>&nbsp;</dd>
      <%}%>
    </script>
</div>
<div class="rule-box" id="RuleBox">
    <i></i>
    <P>红包规则：</P>
    <p>
        1、发红包优先使用您的“密友圈账户余额”。<br/>
        密友圈账户余额，来自你分享商品获得的佣金，或者参加活动获得的奖励；
    </p>

     <p>    
        2、当你的账户余额不足时，还可以通过微信支付充值。<br/>
        根据国家的税法规定和微信支付规则，充值和发放红包需要交纳相应的税费和手续费，在内测期间，密友圈将给予密友补贴，因此只收取5%的手续费。
     </p>

     <p>3、红包发出后24小时没有领完，剩余红包将退回到您的账户中。</p>

     <p>4、领取红包人数不固定，10元红包平均有50人领取。</p>

     <p>5、领取红包后，红包以微信转账方式直接存入领取人的微信钱包中。</p>
    </div>
<script>
    var baseUrl = '{$Think.config.SERVER_HOST}';
    var artifactIns={
        activeIndex:0,
        init:function(){
            this._initEvent();
            this._getFreeList();
        },
        _getFreeList:function(callback){
            var me=this;
            $.req.get("call.json?module=shenqi&customerId={$customerId}&method=getAccoutList",{
                userName:"{$userName}",
                params:{
                    "qId":"{$qId}"
                }
            },function(data){
                if(data){
                    me._renderFree(data,callback);
                }
            });
        },
        _createOrder:function(callback){
            var isRed=0;
            var me=this;
            var current=$("#PayBox dd.current");
            if(current.size()>0) {
                rechange=current.data("rechange");
            }
            $.util.pageLoading.show("加载中");
            $.req.get("call.json?module=shenqi&customerId={$customerId}&method=createRechangeOrder",{
                userName:"{$userName}",
                params:{
                    "qId":"{$qId}",
                    "price":rechange
                }
            },function(data){
                if(data && !data.code){
                    callback && callback(data);
                }
            });
        },
        _payOrder: function(data) {
            this.WxPayParamAjax(0, '账户充值',data.price, 'shenqi', data.id, 0, "{$userName}", 'saveAccountFree');
        },
        /*
        * 获取微信支付参数
        * oid:订单id
        * desc:商品描述显示在微信支付界面中
        * productFee:产品价格
        * module:回调模块
        * moduleId:回调模块id
        * transportFee:运费单位分
        * userName:用户openid
        * method:回调方法
        */
        WxPayParamAjax: function(oid, desc, productFee, module, moduleId, transportFee, userName, method) {
            var urlString = baseUrl + 'Wxpay/getWxPayParam.json?customerId={$customerId}';
            var me=this;
            $.ajax({
                url: urlString,
                dataType: "json",
                type: "post",
                data: {
                    'sn': 0,
                    'desc': desc,
                    'productFee': productFee,
                    'module': module,
                    'moduleId': moduleId,
                    'transportFee': transportFee,
                    'userName': userName,
                    'method': method
                }, success: function(d) {
                    $.util.pageLoading.hide();
                    if (d.code) {
                        $.util.alert(d.msg);
                        return;
                    }
                     WeixinJSBridge.invoke('getBrandWCPayRequest', d.pkg, function(res) {
                        WeixinJSBridge.log(res.err_msg);
                        if (res.err_msg == 'get_brand_wcpay_request:ok') {                                      
                           me._getFreeList(function(){
                                $(".pay dd").eq(me.activeIndex).addClass("current");
                           });
                        } else  {
                            var res = res; 
                            $(".pay dd").eq(me.activeIndex).removeClass("current");
                            if (res.err_msg == 'get_brand_wcpay_request:cancel') {
                                return alert("操作已取消"), void 0
                            } else if (res.err_msg == 'get_brand_wcpay_request:fail') {
                                return alert("网络错误发送失败，请稍后再试"), void 0
                            } else if (res.err_msg == 'access_control:not_allow' || res.err_msg == 'system:access_denied') {
                                return alert("商家尚未开通微信支付"), void 0
                            } else {
                                return alert(res.err_msg), void 0
                            }                                   
                        }
                    });
                }
            });
        },
        _save:function(){
            var me=this;
            var title=$("#title").val();
            var content=$("#content").val();
            var pics=[];
            $("#picDiv img").each(function(i,n){
                pics.push({
                    "picUrl":$(n).data("src"),
                    "picInfo":""
                });
            });
            var isRed=0;
            var total=0;
            var current=$("#PayBox dd.current");
            if(current.size()>0) {
                isRed=1;
                total=current.data("base");
            }
            if(!title) {
                $.util.alert("请输入标题！");
                return;
            }
            function send(){
                $.req.post("call.json?module=shenqi&customerId={$customerId}&method=saveShare",{
                    userName:"{$userName}",
                    "module":"shenqi",
                    "method":"saveShare",
                    params:{
                        "qId":"{$qId}",
                        "title":title,
                        "content":content,
                        "isRed":isRed,
                        "total":total,
                        "pics":JSON.stringify(pics)
                    }
                },function(data){
                    if(data ){
                        if(!data.code) {
                            var p={
                                id:data.id,
                                qId:"{$qId}"
                            };
                            p=encodeURIComponent(JSON.stringify(p));
                            location.href = baseUrl+'call.html?customerId={$customerId}&module=shenqi&method=getView&params=' + p;
                        } else {
                            $.util.alert(data.msg);
                        }
                    }
                });
            }
            var  msg="是否确认发布？";
            if(new RegExp("红.*包","g").test(title)){
               msg='注意：标题中出现“红包”字样，只能分享到微信群，分享到朋友圈会被屏蔽。是否确认继续发布？';
            }
            $.util.ok(msg,function(){
                send();
           });
        },
        _renderFree:function(data,callback){
            var me=this;
           if(data){
                var html=template("PayListTpl", data),
                    container=$("#PayBox");
                container.html(html);
                callback && callback();
                $(".pay dd").click(function(){
                    if($(this).hasClass("current")){
                        $(this).removeClass("current");
                    }else{
                        var index=$(".pay dd").index($(this));
                        me.activeIndex=index;
                        $(this).addClass("current").siblings().removeClass("current");
                        if($(this).hasClass("rechange")) {
                            me._createOrder(function(data){
                                me._payOrder(data);
                            });
                        }
                    }
                });
                $("#view").click(function(){
                    $.util.mask("#000",10,.8);
                    $("#RuleBox").show();
                });
                $("#RuleBox i").click(function(){
                    $("#mask").remove();
                    $("#RuleBox").hide();
                });
           }   
        },
        _uploadCallBack:function(data,status){
            var imgstr='<div class="uploadimgbox">'+
                    '<del onclick="artifactIns.delPic(this)"></del>'+
                    '<img data-src="'+data.path+'" src="'+data.path+'@70w_70h_1e_1c_2o_1sh.jpg">'+
                    '</div>';
            if($("#picDiv .uploadimgbox").size() >=9) {
                return;
            }   
            if($("#picDiv .uploadimgbox").size()<1) {
                $("#picDiv").prepend(imgstr);
            } else {
                $("#picDiv .uploadimgbox:last").after(imgstr);
            }
            if($("#picDiv .uploadimgbox").size() > 8) {
                $("#uploadImage").hide();
            }
        },
        delPic:function(obj){
            $(obj).parent().remove();
            if($("#picDiv .uploadimgbox").size() < 9) {
                $("#uploadImage").show();
            }
        },
        _initEvent:function(){
            var me=this;
            $("#uploadImage").click(function(){
                wx_share.uploadMultImage(function(data,status){
                    me._uploadCallBack(data,status);
                });
            });
            $("#sendBtn").click(function(){
                me._save();
            });
        }
    }
    $(".content textarea").on("input",function(){
        var content=$(".content textarea").val();
            if(content.length>140){
                $.util.alert("文本长度不超过140个字！");
                $(".content textarea").val(content.substr(0,140));
                return;
            }
            $(".txtNum").html(content.length+'/140');
    });
    artifactIns.init();
    wx_share.init({
        "pkg":{
            "appId": '{$pkg.appId}',
            "timestamp": {$pkg.timestamp},
            "nonceStr": '{$pkg.nonceStr}',
            "signature": '{$pkg.signature}'
        },
        hide:1
    });
    $('.face').bind({
            click: function(event){
                if(! $('#sinaEmotion').is(':visible')){
                    $(this).sinaEmotion($("#content"));
                    event.stopPropagation();
                }
            }
        });
</script>
</body>
</html>